﻿//Author:       Le Van Binh
//Date created: 06/05/2013
//Description:  All common functions to use in Index page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using System.Threading;
using System.Globalization;
using Ivs.Core.Common.Utilities;
using SketchTour.Models;
using WebMatrix.WebData;
using SketchTour.DAL;
namespace SketchTour.Controllers.System
{
    public class MSUS00Controller : BaseController
    {
        #region Public Const
        // Set language is English
        public const string m_English = "en-US";
        //Set language is Japanese
        public const string m_Japanese = "ja-JP";

        Dictionary<int, string> m_isActive = new Dictionary<int, string>() { { 1, "InActive" }, { 0, "Active" } };
        #endregion

        private MSUR00DAL service = new MSUR00DAL();
        private void BindStatus(int selectedValue)
        {
            Dictionary<int, string> statuses = new Dictionary<int, string>();
            statuses[0] = "InActive";
            statuses[1] = "Active";
            ViewData["trangThais"] = new SelectList(statuses, "key", "value", selectedValue);

        }
        #region Action Result Method


        /// <summary>
        /// Event page is loading
        /// </summary>
        /// <returns></returns>
        /// @cond
        /// @Author : Le Van Binh
        /// @endcond
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LoginModel model, string returnUrl)
        {
            //if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            if (ModelState.IsValid)
            {

                return View(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
        public ActionResult Management()
        {
            return View();
        }
        //

        //
        public ActionResult Add()
        {
            //ViewData["Status"] = new SelectList(m_isActive, "Key", "Value");
            //ViewBag.IsActive = new SelectList(m_isActive, "Key", "Value");
            //BindStatus(0);
            var sli = new[]{  
                        new SelectListItem {Text = "InActive", Selected = false},  
                        new SelectListItem {Text = "Active", Selected =true },  
            };
            return View();
        }
        [HttpPost]
        public ActionResult Add(MSUR00Models model, string returnUrl)
        {

            //if (ModelState.IsValid)
            //{
            // Attempt to register the user
            try
            {
                //Call fuction insert
                int result = service.Insert(model);
                //Create message to display
                //TempData[CData.MsgContent] = CMessage.DisplayMessageInserted(result);                    
                return View(returnUrl);
            }
            catch
            { }
            //}

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
        //
        public ActionResult Edit()
        {
            return View();
        }
        //
        public ActionResult ChangePassWord()
        {
            return View();
        }
        //
        public ActionResult Logout()
        {
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Function check language 
        /// </summary>
        /// <param name="_ValueLanguage"></param>
        /// <returns></returns>
        /// @cond
        /// Author : Le Van Binh
        /// #endcond
        public ActionResult CheckLanguage(string _ValueLanguage)
        {
            string urlCurent = Request.UrlReferrer.ToString();
            if (_ValueLanguage == m_English)
            {
                //Set language is Japanese in case English
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(m_English);
                Session["CurrentCulture"] = 0;
                //Save the status in case English
                Session["Language"] = m_English;
            }
            else if (_ValueLanguage == m_Japanese)
            {
                //Set language is Japanese in case Japanese
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(m_Japanese);
                Session["CurrentCulture"] = 1;
                //Save the status in case Japanese
                Session["Language"] = m_Japanese;
            }
            else
            {
                //Set default language is English if any conditions is not correct
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(m_English);
                Session["CurrentCulture"] = 0;
                //Save the status in case English
                Session["Language"] = m_English;
            }
            //Link to the Index Page
            return Json(new { success = true, redirectUrl = urlCurent }, JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
